﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DetectScars.common
{
    class LogUtils
    {
        //将字符串写入日志文件
        public static void WriteLog(string strLog)
        {
            string path = Path.Combine(Directory.GetCurrentDirectory(), "log");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            //创建一个当天的日志文件
            string sFilePath = path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            FileInfo fi = new FileInfo(sFilePath);
            if (!fi.Exists)
            {
                using (FileStream fs = fi.Create())
                {
                    // FileStream will be closed automatically when leaving the using block
                }
            }
            using (FileStream fs = new FileStream(sFilePath, FileMode.Append))
            using (StreamWriter sw = new StreamWriter(fs))
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + strLog);
                sw.Close();
            }
        }
    }
}
